package worker

import (
	"NovelBook/logic/book"
	"NovelBook/utils/auth"
	"NovelBook/utils/response"
	"net/http"

	"github.com/gin-gonic/gin"
)

func ReturnBorrowerBooks(c *gin.Context) {
	idAny, exist := c.Get(auth.WorkerIDKey)
	id := idAny.(uint)
	if !exist || id < 1 {
		response.Fail(c, http.StatusUnauthorized, response.CodeTokenInvalid)
		return
	}

	var req struct {
		FinePayed []struct {
			Fine         int64  `json:"fine" binding:"required"`
			BookUniqueID string `json:"book_unique_id" binding:"required"`
		}
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		return
	}

	if err := book.ReturnBooksByUniqueID(req.FinePayed); err != nil {
		response.FailWithMsg(c, http.StatusInternalServerError, response.CodeSystemBusy, err.Error())
		return
	}

	response.Success(c, nil)
}
